package code.tools.commponents
{
	import flash.display.DisplayObjectContainer;
	import flash.events.MouseEvent;
	
	/**
	 * 竖向tab切换标签
	 * @author liangqian
	 */
	public class VTab extends Component
	{
		/**
		 * 当前选中的 tab 的 name
		 */
		private var _selectedIndex:int = -1;

		private var tabItems:Array;

		/**
		 * Tabs 发生改变时候的外部注入钩子onChange(i)
		 */
		private var onChange:Function;
		
		private var _labels:Array;

		/**
		 * @param parent
		 * @param xpos
		 * @param ypos
		 * @param changeHandler changeHandler(selectIndex)
		 * @param labels
		 */
		public function VTab(parent:DisplayObjectContainer ,
							xpos:Number ,
							ypos:Number ,
							changeHandler:Function ,
							... labels)
		{
			super(parent , xpos , ypos);
			onChange = changeHandler;

			tabItems = [];
			dataprovider = labels;
		}

		///////////////////////////////////
		// public methods
		///////////////////////////////////
		public function set dataprovider(labels:Array):void
		{
			clear();
			_labels = labels;
			var len:int  = labels == null ? 0 : labels.length;
			var tabItem:TabItem;
			var ypos:int = 0;
			var i:int;
			for ( ; i < len ; i++)
			{
				tabItem = tabItems[i];
				if (tabItem == null)
				{
					if(labels[i] == null || labels[i] == '')continue;
					tabItem = new TabItem(this , 0 , 0 , labels[i] , tabClickHandler);
					tabItem.keepAlive = true;
					tabItems[i] = tabItem;
					tabItem.tag = i;
				}
				tabItem.visible = true;
				tabItem.label = labels[i];
				tabItem.y = ypos;
				ypos += (tabItem.height + 2);
			}
			len = tabItems.length;
			for(; i < len; i++)
				tabItems[i].visible = false;
//			/* 默认第一个被选中 */
//			selectedIndex = 0;
		}

		private function clear():void
		{
			var len:int = tabItems.length;
			for (var i:int ; i < len ; i++)
				removeChild(tabItems.pop());
		}

		///////////////////////////////////
		// event handlers
		///////////////////////////////////
		/**
		 * tab标签被按下后的统一事件处理方法
		 */
		private function tabClickHandler(e:MouseEvent):void
		{
			selectedIndex = e.currentTarget.tag;
		}

		///////////////////////////////////
		// getter/setters
		///////////////////////////////////
		/**
		 * 返回当前选中的tab的id
		 * @return 当前选中的tab的id
		 */
		public function get selectedIndex():int
		{
			return _selectedIndex;
		}

		public function get selectedTabName():String
		{
			return _labels[selectedIndex];
		}

		/**
		 * 通过指证设置当前选定的 tab
		 * @param index 要选取的tab的指针
		 */
		public function set selectedIndex(index:int):void
		{
			var len:int = tabItems.length;
			var _index:int = Math.max(index,0);
			if (len == 0)
				return;
			for (var i:int ; i < len ; i++)
				tabItems[i].selected = false;
			tabItems[_index].selected = true;
//			if (_selectedIndex == index)
//				return; /* lazy */
			_selectedIndex = _index;
			/* 调用整个tabas的外部处 理函数 */
			onChange(_selectedIndex);
		}
	}
}

import code.structure.manager.loader.DllLoaderManager;
import code.tools.commponents.Component;
import code.tools.commponents.Helpers;
import code.untils.UiNameConst;

import flash.display.DisplayObjectContainer;
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;

class TabItem extends Component
{
	private var labelBitmap:TextField;

	private var tabNormalMc:MovieClip;
	private var tabSelectedMc:MovieClip;
	public function TabItem(parent:DisplayObjectContainer ,
							xpos:int ,
							ypos:int ,
							text:String ,
							onClick:Function)
	{
		super(parent , xpos , ypos);
		label = text;
		addEventListener(MouseEvent.CLICK , onClick);
	}

	override protected function init():void
	{
		setSize(57,45);
		super.init();
		buttonMode = true;
	}

	override protected function addChildren():void
	{
		super.addChildren();
	
		tabNormalMc = new (DllLoaderManager.getInstance().getMaterial(UiNameConst.GENERAL_ICON,'vtab_normal') as Class) as MovieClip;
		tabSelectedMc = new (DllLoaderManager.getInstance().getMaterial(UiNameConst.GENERAL_ICON,'vtab_selected') as Class) as MovieClip;
		
		addChild(tabNormalMc);
		addChild(tabSelectedMc);
		labelBitmap = Helpers.FormatTextField(this,0,4,50,TextFieldAutoSize.CENTER,0x663333);
		labelBitmap.height = 42;
		labelBitmap.selectable = false;
		labelBitmap.mouseEnabled = false;
		labelBitmap.multiline = true;
	} 

	///////////////////////////////////
	// public methods
	///////////////////////////////////
	override public function draw():void
	{
		super.draw();
		if (_selected)
		{
			tabSelectedMc.visible = true;
			tabNormalMc.visible = false;
		}
		else
		{
			tabNormalMc.visible = true;
			tabSelectedMc.visible = false;
		}
//		labelBitmap.x = (_width - (9 * Helpers.checkStringByteLength(labelBitmap.text)));
	}
	///////////////////////////////////
	// event handler
	///////////////////////////////////

	///////////////////////////////////
	// get/set
	///////////////////////////////////
	private var _selected:Boolean;

	public function set selected(value:Boolean):void
	{
		if (_selected == value)
			return;
		_selected = value;
		invalidate();
	}

	public function set label(value:String):void
	{
//		const tabWidth:int = 14 + 9 * Helpers.checkStringByteLength(value);
//		setSize(tabWidth , 20);
		
		labelBitmap.htmlText = value;
	
		invalidate();
	}
}
